package com.cheer.ba.utils;

import android.content.Context;
import android.text.TextUtils;

import com.cheer.ba.R;
import com.cheer.ba.app.App;

/**
 * 币种换算
 */
public class Currency {

    /**
     * 人民币
     */
    public static final String RMB = "156";

    /**
     * 美元
     */
    public static final String USD = "840";

    /**
     * 港币
     */
    public static final String HK = "344";

    /**
     * 日元
     */
    public static final String JPY = "392";

    /**
     * 欧元
     */
    public static final String EURO = "978";

    /**
     * 马来西亚吉特
     */
    public static final String Ringgit = "458";

    /**
     * 澳元
     */
    public static final String AUD = "032";

    /**
     * @param currency 币种
     * @param dollar   钱
     * @param flag     0表示钱，1表示收益
     * @return 返回转化后的钱
     */
    public static String returnDollar(String currency, String dollar, int flag) {
        Context context = App.getContext();
        String money = "";
        if (StringUtils.isEmpty(dollar) || Double.parseDouble(dollar) == 0) {
            if (flag == 0) {
                money = "0.00";
            } else {
                money = "0";
            }
        } else {
            if (flag == 0) {
                // DecimalFormat df = new DecimalFormat("#.00");
                // money = df.format(d);
                double d = Double.parseDouble(dollar) / 100;
                money = String.format("%.2f", d);
            } else {
                money = dollar;
            }
        }
        if (flag == 1) {
            money += context.getString(R.string.point_yu_e) + " ";
        } else if (flag == 0) {
            if (StringUtils.isEmpty(currency)) {
                currency = "156";
            }
            if (TextUtils.equals(currency, RMB)) {
                // money = context.getString(R.string.rmb) + " ";
                money += context.getString(R.string.rmb_yuan) + " ";
            } else if (TextUtils.equals(currency, USD)) {
                money += context.getString(R.string.dollar) + " ";
            } else if (TextUtils.equals(currency, HK)) {
                money += context.getString(R.string.hk) + " ";
            } else if (TextUtils.equals(currency, JPY)) {
                money += context.getString(R.string.yen) + " ";
            } else if (TextUtils.equals(currency, EURO)) {
                money += context.getString(R.string.euro) + " ";
            } else if (TextUtils.equals(currency, Ringgit)) {
                money += context.getString(R.string.Ringgit) + " ";
            } else if (TextUtils.equals(currency, AUD)) {
                money += context.getString(R.string.dollar) + " ";
            }
        }
        return money;
    }

    /**
     * 将分转成元,并去掉小数点后多余的0
     *
     * @param dollar
     * @return
     */
    public static String returnDollar(String dollar) {
        if (StringUtils.isEmpty(dollar)) {
            return "0元";
        }
        Context context = App.getContext();
        String money = "";
        double d = Double.parseDouble(dollar) / 100;
        money = StringUtils.removePoint(d + "") + context.getString(R.string.rmb_yuan) + "";
        return money;
    }

    /**
     * 算出折后优惠
     *
     * @param dollar 分
     * @param sale
     * @return
     */
    public static String returnDollar(String dollar, String sale) {
        if (StringUtils.isEmpty(dollar) || StringUtils.isEmpty(sale)) {
            return "0";
        }
        Context context = App.getContext();
        double d = (Double.parseDouble(dollar) * Double.parseDouble(sale)) / 100;
        return StringUtils.removePoint(d + "");
    }

    /**
     * 算出折后优惠
     *
     * @param dollar 元
     * @param sale
     * @return
     */
    public static String returnDollarPoint(String dollar, String sale) {
        if (StringUtils.isEmpty(dollar) || StringUtils.isEmpty(sale)) {
            return "0";
        }
        Context context = App.getContext();
        double d = Double.parseDouble(dollar) * Double.parseDouble(sale);
        LogUtils.LogD("money----1", d + "========");
        return StringUtils.removePoint(d + "");
    }

    /**
     * 将元转成分,并返回整数
     *
     * @return
     */
    public static String return2Point(String dollar) {
        if (StringUtils.isEmpty(dollar)) {
            return "0";
        }
        Context context = App.getContext();
        int d = (int) (Double.parseDouble(dollar) * 100);
        return d + "";
    }

    /**
     * 保留2位有效数字
     *
     * @param money 元
     */
    public String returnPoint(String money) {
        String m = "";
        if (StringUtils.isEmpty(money)) {
            return "0";
        }
        double d = Double.parseDouble(money);
        m = String.format("%.2f", d);
        return StringUtils.removePoint(m);
    }
}
